<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="平台id" prop="disPlatformId">
        <el-input
          v-model="queryParams.disPlatformId"
          placeholder="请输入平台id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算模型" prop="calModel">
        <el-select v-model="queryParams.calModel" placeholder="请选择计算模型" clearable size="small">
          <el-option
            v-for="dict in calModelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select v-model="queryParams.accountType" placeholder="请选择账户类型" clearable size="small">
          <el-option
            v-for="dict in accountTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="上下级关系" prop="disProLevel">
        <el-input
          v-model="queryParams.disProLevel"
          placeholder="请输入从下往上对应的级别关系"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员类型" prop="disUserType">
        <el-select v-model="queryParams.disUserType" placeholder="请选择会员类型" clearable size="small">
          <el-option
            v-for="dict in disUserTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间" prop="addTime">
        <el-input
          v-model="queryParams.addTime"
          placeholder="请输入添加时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易方式" prop="distTradeMode">
        <el-select v-model="queryParams.distTradeMode" placeholder="请选择交易方式" clearable size="small">
          <el-option
            v-for="dict in distTradeModeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户段位" prop="disUserRank">
        <el-select v-model="queryParams.disUserRank" placeholder="请选择用户段位" clearable size="small">
          <el-option
            v-for="dict in disUserRankOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="身份类型" prop="identityType">
        <el-select v-model="queryParams.identityType" placeholder="请选择身份类型" clearable size="small">
          <el-option
            v-for="dict in identityTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['distribution:param:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['distribution:param:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['distribution:param:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['distribution:param:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paramList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="平台id" align="center" prop="disPlatformId" />
      <el-table-column label="计算模型" align="center" prop="calModel" :formatter="calModelFormat" />
      <el-table-column label="账户类型" align="center" prop="accountType" :formatter="accountTypeFormat" />
      <el-table-column label="分润值" align="center" prop="disProValue" />
      <el-table-column label="上下级关系" align="center" prop="disProLevel" />
      <el-table-column label="会员类型" align="center" prop="disUserType" :formatter="disUserTypeFormat" />
      <el-table-column label="添加时间" align="center" prop="addTime" />
      <el-table-column label="交易方式" align="center" prop="distTradeMode" :formatter="distTradeModeFormat" />
      <el-table-column label="用户段位" align="center" prop="disUserRank" :formatter="disUserRankFormat" />
      <el-table-column label="身份类型" align="center" prop="identityType" :formatter="identityTypeFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['distribution:param:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:param:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分润参数设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="平台id" prop="disPlatformId">
          <el-input v-model="form.disPlatformId" placeholder="请输入平台id" />
        </el-form-item>
        <el-form-item label="计算模型" prop="calModel">
          <el-select v-model="form.calModel" placeholder="请选择计算模型">
            <el-option
              v-for="dict in calModelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账户类型" prop="accountType">
          <el-select v-model="form.accountType" placeholder="请选择账户类型">
            <el-option
              v-for="dict in accountTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分润值" prop="disProValue">
          <el-input v-model="form.disProValue" placeholder="请输入分润值" />
        </el-form-item>
        <el-form-item label="上下级关系" prop="disProLevel">
          <el-input v-model="form.disProLevel" placeholder="请输入从下往上对应的级别关系" />
        </el-form-item>
        <el-form-item label="会员类型" prop="disUserType">
          <el-select v-model="form.disUserType" placeholder="请选择会员类型">
            <el-option
              v-for="dict in disUserTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易方式" prop="distTradeMode">
          <el-select v-model="form.distTradeMode" placeholder="请选择交易方式">
            <el-option
              v-for="dict in distTradeModeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户段位" prop="disUserRank">
          <el-select v-model="form.disUserRank" placeholder="请选择用户段位">
            <el-option
              v-for="dict in disUserRankOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份类型" prop="identityType">
          <el-select v-model="form.identityType" placeholder="请选择身份类型">
            <el-option
              v-for="dict in identityTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listParam, getParam, delParam, addParam, updateParam, exportParam } from "@/api/distribution/param";

    export default {
        name: "param",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 分润参数设置表格数据
                paramList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 计算模型，如 百分比和固定金额字典
                calModelOptions: [],
                // 账户类型，如上级发展下级分润 ，交易分润。。。。字典
                accountTypeOptions: [],
                // 会员类型字典
                disUserTypeOptions: [],
                // 交易方式(分润或者提现)字典
                distTradeModeOptions: [],
                // 用户段位字典
                disUserRankOptions: [],
                // 身份类型(0,会员，1：平台商)字典
                identityTypeOptions: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    disPlatformId: null,
                    calModel: null,
                    accountType: null,
                    disProValue: null,
                    disProLevel: null,
                    disUserType: null,
                    addTime: null,
                    distTradeMode: null,
                    disUserRank: null,
                    identityType: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    calModel: [
                        { required: true, message: "计算模型，如 百分比和固定金额不能为空", trigger: "change" }
                    ],
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("dis_cal_model").then(response => {
                this.calModelOptions = response.data;
            });
            this.getDicts("dis_account_type").then(response => {
                this.accountTypeOptions = response.data;
            });
            this.getDicts("dis_member_type").then(response => {
                this.disUserTypeOptions = response.data;
            });
            this.getDicts("dis_trade_model").then(response => {
                this.distTradeModeOptions = response.data;
            });
            this.getDicts("dis_user_rank").then(response => {
                this.disUserRankOptions = response.data;
            });
            this.getDicts("dis_identity_type").then(response => {
                this.identityTypeOptions = response.data;
            });
        },
        methods: {
            /** 查询分润参数设置列表 */
            getList() {
                this.loading = true;
                listParam(this.queryParams).then(response => {
                    this.paramList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 计算模型，如 百分比和固定金额字典翻译
            calModelFormat(row, column) {
                return this.selectDictLabel(this.calModelOptions, row.calModel);
            },
            // 账户类型，如上级发展下级分润 ，交易分润。。。。字典翻译
            accountTypeFormat(row, column) {
                return this.selectDictLabel(this.accountTypeOptions, row.accountType);
            },
            // 会员类型字典翻译
            disUserTypeFormat(row, column) {
                return this.selectDictLabel(this.disUserTypeOptions, row.disUserType);
            },
            // 交易方式(分润或者提现)字典翻译
            distTradeModeFormat(row, column) {
                return this.selectDictLabel(this.distTradeModeOptions, row.distTradeMode);
            },
            // 用户段位字典翻译
            disUserRankFormat(row, column) {
                return this.selectDictLabel(this.disUserRankOptions, row.disUserRank);
            },
            // 身份类型(0,会员，1：平台商)字典翻译
            identityTypeFormat(row, column) {
                return this.selectDictLabel(this.identityTypeOptions, row.identityType);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    disPlatformId: null,
                    calModel: null,
                    accountType: null,
                    disProValue: null,
                    disProLevel: null,
                    disUserType: null,
                    isDelete: null,
                    updateTime: null,
                    addTime: null,
                    distTradeMode: null,
                    disUserRank: null,
                    identityType: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加分润参数设置";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getParam(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改分润参数设置";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateParam(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addParam(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除分润参数设置编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return delParam(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有分润参数设置数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return exportParam(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        }
    };
</script>
